package com.guaju.yueshangLife.widget;

import android.app.Activity;
import android.databinding.DataBindingUtil;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.guaju.yueshangLife.R;
import com.guaju.yueshangLife.databinding.BottomShareDialogBinding;
import com.guaju.yueshangLife.share.WXShareUtil;


public class BottomShareDialog {
    Activity activity;
    MyDialog dialog = null;
    private static BottomShareDialog shareDialog;
    private static BottomShareDialogBinding dialogBinding;
    private int sharedCount=0;

    private BottomShareDialog(Activity activity, String shareTitle, String shareDes, String shareIcon, String shareLink, String wxPorid, String share_lite_link,String share_friend_type,String share_group_type,String big_image) {
        this.activity = activity;
        generateShareDialog(shareTitle, shareDes, shareIcon, shareLink, wxPorid, share_lite_link,share_friend_type,share_group_type,big_image);
    }

    public static BottomShareDialog getInstance(Activity activity, String shareTitle, String shareDes, String shareIcon, String shareLink, String wxPorid, String share_lite_link,String share_friend_type,String share_group_type,String big_image)  {
        if (shareDialog == null) {
            shareDialog = new BottomShareDialog(activity, shareTitle, shareDes, shareIcon, shareLink, wxPorid, share_lite_link,share_friend_type,share_group_type,big_image);
        }
        return shareDialog;
    }

    public static BottomShareDialog getInstance(Activity activity, String shareTitle, String shareDes, String shareIcon, String shareLink)  {
        if (shareDialog == null) {
            shareDialog = new BottomShareDialog(activity, shareTitle, shareDes, shareIcon, shareLink,null,null,null,null,null);
        }
        return shareDialog;
    }

    public void show() {
        if (dialog != null&&!dialog.isShowing()) {
            dialog.show();
        }
    }

    public void hide() {
        if (dialog != null&&dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    public void generateShareDialog(final String shareTitle, final String shareDes, final String shareIcon, final String shareLink, final String wxPorid, final String share_lite_link, final String share_friend_type, final String share_group_type, final String big_image) {
        dialogBinding = DataBindingUtil.inflate(LayoutInflater.from(activity), R.layout.bottom_share_dialog, (ViewGroup) activity.getWindow().getDecorView(), false);
        dialogBinding.ivShareWxcircle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                TODO wechat share
//                if (!DoubleClickUtil.isDoubleClick(2000)) {
                dialogBinding.ivShareWxcircle.setClickable(false);
//                WXShareUtil.shareLiveToWX(activity,shareLink,shareIcon,shareTitle,shareDes,wxPorid,share_lite_link, SendMessageToWX.Req.WXSceneSession);
//                WXShareUtil.shareLiveToWX(activity, shareLink, shareIcon, shareTitle, shareDes, wxPorid, share_lite_link, SendMessageToWX.Req.WXSceneTimeline);
//                WXShareUtil.shareURLToWX(activity,shareLink, shareIcon, shareTitle, shareDes, WXShareUtil.WX_CIRCLE);
                /**
                 * 0 分享链接 1 分享图片
                 */
                if (TextUtils.equals("0",share_group_type)||TextUtils.isEmpty(share_group_type)){
                    boolean isMantual=true;
                    sharedCount++;
                    Log.e("guajujerry", "onClick: 分享了"+sharedCount);
                    WXShareUtil.setSharedCount(0);
                    WXShareUtil.shareURLToWX(activity,shareLink, shareIcon, shareTitle, shareDes, WXShareUtil.WX_CIRCLE);
                }else if (TextUtils.equals("1",share_group_type)){
                    WXShareUtil.setSharedCount(0);
                    WXShareUtil.shareToWX(activity,null,big_image,shareTitle,null,null,null,WXShareUtil.WX_CIRCLE,1);
                }

                shareDialog.hide();
                dialogBinding.ivShareWxcircle.setClickable(true);
            }
        });
        dialogBinding.ivShareWxchat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                TODO quan share
//                if (!DoubleClickUtil.isDoubleClick(2000)) {
                dialogBinding.ivShareWxchat.setClickable(false);
                /**
                 * 0 1 2 链接 图片 小程序
                 */
                if (TextUtils.equals("0",share_friend_type)||TextUtils.isEmpty(share_friend_type)){
                    boolean isMantual=true;
                    WXShareUtil.setSharedCount(0);
                    WXShareUtil.shareURLToWX(activity,shareLink, shareIcon, shareTitle, shareDes, WXShareUtil.WX_FRIENDS);
                }else if (TextUtils.equals("1",share_friend_type)){
                    WXShareUtil.setSharedCount(0);
                    WXShareUtil.shareToWX(activity,null,big_image,shareTitle,null,null,null,WXShareUtil.WX_FRIENDS,1);
                }else if (TextUtils.equals("2",share_friend_type)){
                    WXShareUtil.setSharedCount(0);
                    WXShareUtil.shareToWX(activity,shareLink,shareIcon,shareTitle,null,wxPorid,share_lite_link,WXShareUtil.WX_FRIENDS,2);
                }
                shareDialog.hide();
                dialogBinding.ivShareWxchat.setClickable(true);
//                } else {
//                    shareDialog.hide();
//                }
            }
        });
        dialogBinding.cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog = new MyDialog(activity, dialogBinding.getRoot(), R.style.BottomDialog);
        Window window = dialog.getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.setWindowAnimations(R.style.BottomDialog_Animation);
        dialog.setContentView(dialogBinding.getRoot());
    }

    public void release() {
        if (dialog != null) {
            dialog.dismiss();
            dialog = null;
            shareDialog = null;
            WXShareUtil.release();
        }

    }

    public void setWidth(int width) {
        dialogBinding.title.setText("分享");
        dialogBinding.title.setTextColor(Color.parseColor("#888888"));
        dialogBinding.cancel.setTextColor(Color.parseColor("#333333"));
        if (dialog != null) {
            Window window = dialog.getWindow();
            WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
            lp.height = activity.getResources().getDimensionPixelOffset(R.dimen.dp_224);
            lp.width = width;
            window.setAttributes(lp);
        }
    }
}
